using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.SceneManagement;

namespace survivor
{
	public class TestUIStartPanelData : UIPanelData
	{
	}
	public partial class TestUIStartPanel : UIPanel
	{
		protected override void OnInit(IUIData uiData = null)
		{
			mData = uiData as TestUIStartPanelData ?? new TestUIStartPanelData();
            // please add init code here
            Time.timeScale = 1;
            BtnCoinUpgrade.onClick.AddListener(() =>
            {
                CoinUpgradePanel.Show();
            });

            BtnStartGameUpgrade.onClick.AddListener(()=>{
                Global.Cleanup();
                this.CloseSelf();
                SceneManager.LoadScene("game");
            });

            Global.Coin.RegisterWithInitValue(coin => {

                CoinText.text = "金币：" + coin;
                if (coin >= 5)
                {
                    BtnExpPercenUpgrade.Show();
                    BtnCoinPercenUpgrade.Show();
                }
                else
                {
                    BtnExpPercenUpgrade.Hide();
                    BtnCoinPercenUpgrade.Hide();
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            BtnCoinPercenUpgrade.onClick.AddListener(() => {
                Global.CoinPercent.Value += 0.1f;
                Global.Coin.Value -= 5;
            });

            BtnExpPercenUpgrade.onClick.AddListener(() => {
                Global.ExpPercent.Value += 0.1f;
                Global.Coin.Value -= 5;
            });

            BtnClose.onClick.AddListener(() => {
                CoinUpgradePanel.Hide();
            });
        }
		
		protected override void OnOpen(IUIData uiData = null)
		{
		}
		
		protected override void OnShow()
		{
		}
		
		protected override void OnHide()
		{
		}
		
		protected override void OnClose()
		{
            // 确保所有事件监听器都被清理
            BtnCoinUpgrade.onClick.RemoveAllListeners();
            BtnStartGameUpgrade.onClick.RemoveAllListeners();
            BtnCoinPercenUpgrade.onClick.RemoveAllListeners();
            BtnExpPercenUpgrade.onClick.RemoveAllListeners();
            BtnClose.onClick.RemoveAllListeners();
		}
	}
}
